﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms.DanhMuc
{
    public partial class FR_CaLamViec : DevExpress.XtraEditors.XtraForm
    {
        string ma_hoc_van = "";
        string new_id = "";
        bool isUpdate = false;
        IDIC_SHIFTData _IDIC_SHIFTData;
        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_CaLamViec(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_CaLamViec(bool _isUpdate, string mahocvan)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_hoc_van = mahocvan;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IDIC_SHIFTData = (IDIC_SHIFTData)Activator.GetObject(typeof(IDIC_SHIFTData), util.ChuoiDIC_SHIFTData);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();

            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }

        private void tangid()
        {
            new_id = _icommon.Generate_ID_1_Char("DIC_SHIFT", "ShiftCode", "C");
        }

        private void loadthongtin()
        {
            DIC_SHIFT hv = Laythongtin(ma_hoc_van);
            txtma.Text = hv.ShiftCode;
            txtten.Text = hv.ShiftName;
            txtghichu.Text = hv.Description;
            te_den1.Time = hv.BeginTime1;
            te_den2.Time = hv.BeginTime2;
            te_giovaotu1.Time = hv.EndTime1;
            te_giovaotu2.Time = hv.EndTime2;
            te_giora.Time = hv.EndTime;
            te_giovao.Time = hv.BeginTime;
            cal_ditre.Value = hv.LateMinute;
            cal_vesom.Value = hv.EarlyMinute;
            chkisnight.Checked = hv.IsOvernight;
        }

        private DIC_SHIFT Laythongtin(string educode)
        {
            DataSet ds = _IDIC_SHIFTData.DIC_SHIFT_Get(educode);

            DIC_SHIFT hv = new DIC_SHIFT();

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string ten = ds.Tables[0].Rows[0][1].ToString();
                DateTime BeginTime = DateTime.Parse(ds.Tables[0].Rows[0][2].ToString());
                DateTime EndTime = DateTime.Parse(ds.Tables[0].Rows[0][3].ToString());
                bool IsOvernight = bool.Parse(ds.Tables[0].Rows[0][4].ToString());
                DateTime BeginTime1 = DateTime.Parse(ds.Tables[0].Rows[0][5].ToString());
                DateTime BeginTime2 = DateTime.Parse(ds.Tables[0].Rows[0][6].ToString());
                DateTime EndTime1 = DateTime.Parse(ds.Tables[0].Rows[0][7].ToString());
                DateTime EndTime2 = DateTime.Parse(ds.Tables[0].Rows[0][8].ToString());
                int LateMinute =int.Parse( ds.Tables[0].Rows[0][9].ToString());
                int EarlyMinute = int.Parse(ds.Tables[0].Rows[0][10].ToString());
                string ghichu = ds.Tables[0].Rows[0][11].ToString();

                hv.Description = ghichu;
                hv.ShiftCode = ma;
                hv.ShiftName = ten;
                hv.BeginTime = BeginTime;
                hv.BeginTime1 = BeginTime1;
                hv.BeginTime2 = BeginTime2;
                hv.EndTime = EndTime;
                hv.EndTime1 = EndTime1;
                hv.EndTime2 = EndTime2;
                hv.IsOvernight = IsOvernight;
                hv.LateMinute = LateMinute;
                hv.EarlyMinute = EarlyMinute;
                
            }

            return hv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            DateTime BeginTime = te_giovao.Time;
            DateTime EndTime = te_giovao.Time;
            bool IsOvernight = chkisnight.Checked;
            DateTime BeginTime1 = te_giovaotu1.Time;
            DateTime BeginTime2 = te_giovaotu2.Time;
            DateTime EndTime1 = te_giovao.Time;
            DateTime EndTime2 = te_giovao.Time;
            int LateMinute = int.Parse(cal_ditre.Value.ToString());
            int EarlyMinute = int.Parse(cal_vesom.Value.ToString());
            string ghichu = txtghichu.Text;

            int kq = _IDIC_SHIFTData.DIC_SHIFT_Insert(ma, ten, BeginTime,EndTime, IsOvernight, BeginTime1, BeginTime2, EndTime1, EndTime2, LateMinute, EarlyMinute, ghichu);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            DateTime BeginTime = te_giovao.Time;
            DateTime EndTime = te_giovao.Time;
            bool IsOvernight = chkisnight.Checked;
            DateTime BeginTime1 = te_giovaotu1.Time;
            DateTime BeginTime2 = te_giovaotu2.Time;
            DateTime EndTime1 = te_giovao.Time;
            DateTime EndTime2 = te_giovao.Time;
            int LateMinute = int.Parse(cal_ditre.Value.ToString());
            int EarlyMinute = int.Parse(cal_vesom.Value.ToString());
            string ghichu = txtghichu.Text;

            int kq = _IDIC_SHIFTData.DIC_SHIFT_Update(ma, ten, BeginTime, EndTime, IsOvernight, BeginTime1, BeginTime2, EndTime1, EndTime2, LateMinute, EarlyMinute, ghichu);
            return kq;
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtghichu.Text = "";
            txtma.Text = new_id;
            txtten.Text = "";
            te_den1.Time = DateTime.Now;
            te_den2.Time = DateTime.Now;
            te_giovaotu1.Time = DateTime.Now;
            te_giovaotu2.Time = DateTime.Now;
            te_giora.Time = DateTime.Now;
            te_giovao.Time = DateTime.Now;
            cal_ditre.Text = "0";
            cal_vesom.Text= "0";

            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}